package controller;

import java.util.Date;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.HandlerExecutionChain;

import pojo.Book;
import service.WorkService;

@RestController
@RequestMapping(value="/books")
public class BookController {
	@Autowired
	private WorkService workService;
	@GetMapping("addCookie")
	public String addCookie(HttpServletResponse resp) {
		workService.work("JohnYu");
		Cookie cookie=new Cookie("uname", "johnYu");
		cookie.setHttpOnly(true);
		cookie.setMaxAge(100);
		resp.addCookie(cookie);
		return "cookie add success!";
	}
	
	@PutMapping("work1")
	public String work1(@RequestParam(value="bid") int id,String name) {
		
		return name+"..."+id;
	}
	
	@GetMapping("work2")
	public String work2(@CookieValue("uname") String cn) {
		
		return cn+"....";
	}
	
	@GetMapping("{bid}/author/{aid}")
	public String work3(@PathVariable("bid") int bookId,@PathVariable("aid") int authorId) {
		
		return bookId+"...."+authorId;
	}
	
	@PostMapping("json1")
	public Book work4(@RequestBody Book book) {
		
		return book;
		
	}
	
	@PostMapping("birth")
	public String work4(Date birth,@NumberFormat(pattern="##,###.00") double salary) {
		
		return birth.toString();
		
	}
	
}
